using System;
using System.Collections;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Modules.InnerCity
{
    public class TileCollideItem : MonoBehaviour
    {
        public Vector2 size = new Vector2(0.5f, 0.5f);

        public int priority;
        
        public bool is_active = true;

        public SpriteRenderer mainSprite;
        public SpriteRenderer shadowSprite;
        public SpriteRenderer lightingSprite;
        public BoxCollider buildCollider;
        
        public Transform activeScaleItem;
        private Vector3 _defaultScale;
        public Vector3 defaultScale
        {
            get { return _defaultScale; }
            set
            {
                _defaultScale = value;
            }
        }

        private Vector3 initPos;

        private TileCollideManager m_tile_collide_manager;

        private RotateRect m_rect = new RotateRect(Vector2.zero, Vector2.one);
        

        public void SetTileCollideManager(TileCollideManager mgr)
        {
            //defaultScale = activeScaleItem.localScale;
            this.m_tile_collide_manager = mgr;
        }

        public void SetPriority(int priority)
        {
            this.priority = priority;
        }
        
        public void ManualRegister(TileCollideManager collideManager)
        {
            if (collideManager != null)
            {
                collideManager.Add(this);
            }
        }
        
        public void ResetInitLocalPos( Vector3 localPos)
        {
            this.initPos = localPos;
        }
        

        public void Activate()
        {
            // if (!this.is_active)
            // {
            //     this.is_active = true;
            //     activeScaleItem.localScale = defaultScale;
            //     //base.gameObject.SetActive(true);
            // }
        }

        public void Deactivate()
        {
            // if (this.is_active)
            // {
            //     this.is_active = false;
            //     //activeScaleItem.localScale = Vector3.zero;
            //     //base.gameObject.SetActive(false);
            // }
        }
        

        public void OnDespawn()
        {
            if (this.m_tile_collide_manager)
            {
                this.m_tile_collide_manager.Remove(this);
            }
        }

        private void OnDestroy()
        {
            this.OnDespawn();
        }

        public void ShiftPos(float linerValue)
        {
            base.transform.localPosition = Vector3.Lerp(this.initPos, Vector3.zero, linerValue);
        }

        public RotateRect GetGlobalRect()
        {
            Vector2 vector = this.CalSizeScaled();
            this.m_rect.UpdateRect(new Vector2(transform.localPosition.x, transform.localPosition.z), vector);
            return this.m_rect;
        }

        public Vector2 CalSizeScaled()
        {
            return new Vector2(size.x * transform.localScale.x, size.y * transform.localScale.z);
        }
        
        public void SetScale(float scale)
        {
            //transform.localScale = new Vector3(scale, scale, scale);
            //transform.localScale = new Vector3(0.15f,0.15f,0.15f);
        }

        

        private void DrawRect2D()
        {
            RotateRect globalRect = this.GetGlobalRect();
            Vector3 vector = new Vector3(globalRect.world_corners[0].x, 0f, globalRect.world_corners[0].y);
            Vector3 vector2 = new Vector3(globalRect.world_corners[1].x, 0f, globalRect.world_corners[1].y);
            Vector3 vector3 = new Vector3(globalRect.world_corners[2].x, 0f, globalRect.world_corners[2].y);
            Vector3 vector4 = new Vector3(globalRect.world_corners[3].x, 0f, globalRect.world_corners[3].y);
            Gizmos.DrawLine(vector, vector2);
            Gizmos.DrawLine(vector2, vector3);
            Gizmos.DrawLine(vector3, vector4);
            Gizmos.DrawLine(vector4, vector);
        }

        public void OnDrawGizmos()
        {
            Gizmos.color = Color.yellow;
            this.DrawRect2D();
        }

        public void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.red;
            this.DrawRect2D();
        }


    }
}